define(function (require) {
var zsApp = require('app');    
require('selectCache');
zsApp.controller('sysConfigController', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {
	
	// pagination
	$scope.paginationConf = {
        currentPage     : 1,
        totalItems      : 10,
        itemsPerPage    : 20,
        pagesLength     : 15,
        perPageOptions  : [10, 20, 30, 40, 50],
        onChange: function() { 
        	var self = this,
	        	pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	
			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/admin/sysConfig/getList',
				method 	: 'POST',
				data 	:  {
                    pageNumber  : self.currentPage,  
                    pageSize    : self.itemsPerPage,  
                }

			}).success(function(res) { 
				console.log(res);
                if( 0 == res.length ) { 
                    return false;
                }
                // 系统配置键列表页面表格
                $scope.tabVal = res.result;
                angular.forEach($scope.tabVal, function(obj, i) {
                    // 遍历每条数据对应的ID
                    $scope.dataID[i]   = obj.sysConfigId;
                });
                // 总共有多少条数据
                self.totalItems = res.length;
            }).error(function(err) {
            	console.log(err);
            });
        }
    };
	
	// 确定新增
	$scope.submitNewlyAdded = function() {
		console.log($scope.details_syskey);
		// 新增信息（封装成JSON）
		var sysConfigData = {
			sysKey   : $scope.sysconfig_sysKey,
			sysValue   : $scope.sysconfig_sysValue,
			sysStatus : $scope.sysconfig_sysStatus,
			displayName  : $scope.sysconfig_displayName,
			description   : $scope.sysconfig_description,
			
		}
		$http
		({
			url    : $scope.zsUrl+'digitalhome-boss/admin/sysConfig/add',
			method : 'POST',
			data   : sysConfigData, 
 
 			
		}).success(function(res) {

			if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

		}).error(function(err) {
			promptCache.errorPrompt('请求失败！');
            return false;
		});

		// 关闭窗口
		$('#newlyAdded').modal('hide');
	};

	
	 // details
    $scope.details = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/sysConfig/getById/'+$scope.dataID[i],
    		method	: 'GET',

    	}).success(function(res) {
    		if( '0000' != res.result_code ) {
    			return false;
    		}
    		$scope.details_sysKey = res.result.sysKey;
    		$scope.details_sysValue = res.result.sysValue;
    		var status = res.result.sysStatus;
    		if(status == '1'){
    			status = "启用";
    		}else{
    			status = "禁用";
    		}
    		$scope.details_sysStatus =status; 
    		$scope.details_displayName = res.result.displayName;
    		$scope.details_description = res.result.description;
    	}).error(function(err) {
    	});
    };
	
	 // 修改
	 $scope.edit = function(i) {
    	$http
    	({
    		url 	: $scope.zsUrl+'digitalhome-boss/admin/sysConfig/getById/'+$scope.dataID[i],
    		method	: 'GET',
    	}).success(function(res) {
    		if( '0000' != res.result_code ) {
    			return false;
    		}
    		$scope.index = i;
    		$scope.edit_sysKey = res.result.sysKey;
    		$scope.edit_sysValue = res.result.sysValue;
    		var status = res.result.sysStatus;	    		
    		$scope.edit_sysStatus=status;//id的值，区分类型
    		$scope.edit_displayName = res.result.displayName;
    		$scope.edit_description = res.result.description;
    		$scope.editSysStatusSelect = 
    		[
    			{ 'id' : " ", 'name': '请选择' },
    			{ 'id' : 0, 'name': '禁用' },
    			{ 'id' : 1, 'name': '启用' },
    		];
    	}).error(function(err) {
    	});
    };

	// 确认修改（用于修改原有的配置键信息）
	$scope.confirmEdit = function($event) {
         
		// 修改键、值、名称、描述（封装成JSON）
		var editData = {
		    sysConfigId : $scope.dataID[$scope.index],
            sysKey         : $scope.edit_sysKey,
            sysValue       : $scope.edit_sysValue,
            sysStatus : $scope.edit_sysStatus,
			displayName : $scope.edit_displayName,
			description : $scope.edit_description,
		}
		$http(
		{
			url   	: $scope.zsUrl+'digitalhome-boss/admin/sysConfig/modify',
			method  : 'POST',
			data    : editData, 
		}).success(function(res) {

			if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

		}).error(function(err) {
			promptCache.errorPrompt('请求失败！');
            return false;
		});
	};
	
    // delete
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'sysConfigId';
    	// param
    	var deleteUrl = 'admin/sysConfig/delete';
    	// enable disabled
        var isEnableUrl = 'admin/sysConfig/modifyStatus';

    	
    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        //  Single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.sysConfigId;        
            selectCache.select(selectId, bool, len);    
        }; 

        // check
        $scope.check= function () {
            selectCache.check();
        };

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, deleteUrl);
            reloadTable();
        };

        // enable
        $scope.enable= function () { 
            selectCache.isEnable(whichId, isEnableUrl, 1);
            reloadTable();
        };

        // disabled
        $scope.disabled= function () { 
            selectCache.isEnable(whichId, isEnableUrl, 0);
            reloadTable();
        };
    };

    // reload table
    function reloadTable() {
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])

.directive('sysconfigNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/system/sysconfig/newlyAdded.html',
		replace			: true,
	}
})
.directive('sysconfigDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/system/sysconfig/details.html',
		replace			: true,
	}
})
.directive('sysconfigEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/system/sysconfig/edit.html',
		replace			: true,
	}
})
.directive('sysconfigDelete', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/system/sysconfig/delete.html',
		replace			: true,
	}
})
.directive('sysconfigTable', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/system/sysconfig/table.html',
		replace			: true,
	}
});
});